package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.anno.Log;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentsServiceImpl implements StudentsService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageBean findAll(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
        List<Student> students = studentMapper.selectPage(studentQueryParam);
        Page<Student> page = (Page<Student>) students;
        return new PageBean(page.getTotal(),page.getResult());
    }
    @Log
    @Override
    public void delete(Integer[] ids) {
         studentMapper.deleteByIds(ids);
    }
    @Log
    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.add(student);
    }

    @Override
    public void violation(Integer id, Integer score) {
        Student student = studentMapper.getInfoById(id);
        //得到分数并扣分
        Short violationScore = student.getViolationScore();
        short afterScore = (short) (violationScore + score);
        //得到次数
        Short violationCount = student.getViolationCount();
        short afterCount = (short) (violationCount + 1);

        student.setViolationCount(afterCount);
        student.setViolationScore(afterScore);
        studentMapper.update(student);


    }

    @Override
    public Object getInfoById(Integer id) {
        return studentMapper.getInfoById(id);
    }


    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);


    }

//    @Override
//    public void delete(Integer id) {
//        studentMapper.delete(id);
//    }
}
